package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.*;
import com.guigu.pojo.*;
import com.guigu.service.WarehouseDetailsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseDetailsInfoServiceimpl extends ServiceImpl<WarehouseDetailsInfoMapper, WarehouseDetailsInfo> implements WarehouseDetailsInfoService {
    @Autowired
    WarehouseInfoMapper warehouseInfoMapper;//仓库表
    @Autowired
    WarehouseDetailsInfoMapper warehouseDetailsInfoMapper;//仓库详情表
    @Autowired
    SupplierSupplyOfGoodsMapper supplierSupplyOfGoodsMapper;//供应商可提供的商品表
    @Autowired
    UsersMapper usersMapper;//用户表
    @Autowired
    ShopInfoMapper shopInfoMapper;//商品表
    @Autowired
    ShopTypeInfoMapper shopTypeInfoMapper;//商品类型表

    //分页查询
    @Override
    public Page<WarehouseDetailsInfo> queryAll(WarehouseDetailsInfo warehouseDetailsInfo, Integer pageno, Integer pagesize) {
        QueryWrapper queryWrapper=new QueryWrapper<WarehouseDetailsInfo>();
        //条件查询
        //父仓库id
        if (warehouseDetailsInfo.getPId()!=null){
            queryWrapper.eq("p_id",warehouseDetailsInfo.getPId());
        }
        //审核状态
        if (warehouseDetailsInfo.getIsCheck()!=null){
            queryWrapper.eq("is_check",warehouseDetailsInfo.getIsCheck());
        }
        Page<WarehouseDetailsInfo> page = this.page(new Page<WarehouseDetailsInfo>(pageno, pagesize), queryWrapper);
        //循环补齐属性
        for(WarehouseDetailsInfo w : page.getRecords()){
            //获取父仓库对象
            w.setWarehouseInfo(warehouseInfoMapper.selectById(w.getPId()));
            //获取商品对象和商品分类对象
            ShopInfo shopInfo=shopInfoMapper.selectById(w.getShopId());
            shopInfo.setType(shopTypeInfoMapper.selectById(shopInfo.getShopType()));
            w.setShopInfo(shopInfo);
            //获取供应商用户对象
            w.setUsers(usersMapper.selectById(w.getUId()));
            //获取供应商商品对象
            queryWrapper= new QueryWrapper<SupplierSupplyOfGoods>();
            queryWrapper.eq("p_id",w.getUId());
            queryWrapper.eq("g_id",w.getShopId());
            w.setSupplierSupplyOfGoods(supplierSupplyOfGoodsMapper.selectOne(queryWrapper));
        }
        return page;
    }

    //将供应商商品配置到仓库详情内，需要审核
    @Override
    public Map configWarehouseInfo(WarehouseDetailsInfo warehouseDetailsInfo) {
        //当前库存
        warehouseDetailsInfo.setAmount(0);
        //锁定库存
        warehouseDetailsInfo.setLockStock(0);
        //审核状态
        warehouseDetailsInfo.setIsCheck(0);
        //进行数据添加
        Map<String,Object> map=new HashMap<>();
        //进行添加
        if(warehouseDetailsInfoMapper.insert(warehouseDetailsInfo)>0){
            map.put("msg","成功配置到该仓库，等待审核");
            map.put("x",true);
        }else{
            map.put("msg","操作失败！");
            map.put("x",false);
        }
        return map;
    }

    //商品分配到仓库的审核【仓库商品配置审批】
    @Override
    public Map checkWarehouseDetailsInfo(WarehouseDetailsInfo warehouseDetailsInfo) {
        Map<String,Object> map=new HashMap<>();
        //进行修改
        if(warehouseDetailsInfoMapper.updateById(warehouseDetailsInfo)>0){
            //判断结果
            if(warehouseDetailsInfo.getIsCheck()==2){
                map.put("msg","拒绝通过！");
                //删除该条仓库详情数据
                warehouseDetailsInfoMapper.deleteById(warehouseDetailsInfo.getId());
            }else {
                map.put("msg","允许通过！");
            }
            map.put("x",true);
        }else{
            map.put("msg","操作失败！");
            map.put("x",false);
        }
        return map;
    }

    @Override
    public Page queryAllByPId(WarehouseDetailsInfo warehouseDetailsInfo, Integer pageno, Integer pagesize) {

            QueryWrapper<WarehouseDetailsInfo> queryWrapper=new QueryWrapper<WarehouseDetailsInfo>();
            queryWrapper.eq("p_id",warehouseDetailsInfo.getPId());
            queryWrapper.eq("is_check","1");
            Page<WarehouseDetailsInfo> page=this.page(new Page<WarehouseDetailsInfo>(pageno,pagesize),queryWrapper);
            for (WarehouseDetailsInfo  w:page.getRecords()){
                w.setShopInfo(shopInfoMapper.selectById(w.getShopId()));
                w.setUsers(usersMapper.selectById(w.getUId()));
            }

        return page;
    }

    @Override
    public com.guigu.service.utils.Page queryAllshopid(WarehouseDetailsInfo warehouseDetailsInfo, Integer pageno, Integer pagesize) {
        List<WarehouseInfo> listd=new ArrayList<>();
        QueryWrapper<WarehouseInfo> queryWrapper=new QueryWrapper();
        queryWrapper.eq("is_check","1");
        queryWrapper.eq("is_delete","0");
        queryWrapper.ne("id",warehouseDetailsInfo.getPId());
        List<WarehouseInfo> list=warehouseInfoMapper.selectList(queryWrapper);

        for (WarehouseInfo l: list){
            QueryWrapper<WarehouseDetailsInfo> queryWrapperDetails=new QueryWrapper();
            queryWrapperDetails.eq("is_check","1");
            queryWrapperDetails.eq("p_id",l.getId());
            queryWrapperDetails.eq("shop_id",warehouseDetailsInfo.getShopId());
            List<WarehouseDetailsInfo> warehouseDetailsInfoList=warehouseDetailsInfoMapper.selectList(queryWrapperDetails);
            if(warehouseDetailsInfoList.size()>0){
                l.setWarehouseDetailsInfoList(warehouseDetailsInfoList);
                listd.add(l);
            }
        }
        com.guigu.service.utils.Page page=new com.guigu.service.utils.Page();
        page=page.pageList(listd,pageno,pagesize);
        return page;
    }

    @Override
    @Transactional
    //Transactional  回滚  只要你这个方法  出现一个错误就 把抛出异常
    public Map warehuseDetailsinfoallocate(Integer aid, Integer bid, Integer max) {
        Map map=new HashMap();
        map.put("code","0");
        map.put("msg","存入失败");
        if(aid!=null&&bid!=null&&max!=null){
            //要取货的仓库，当前库存=当前库存-取走的数量
            WarehouseDetailsInfo w1=this.getById(aid);
            w1.setAmount(w1.getAmount()-max);
            boolean a=this.updateById(w1);
            //要存放的仓库，当前库存=当前库存+存放的数量
            WarehouseDetailsInfo w2=this.getById(bid);
            w2.setAmount(w2.getAmount()+max);
            boolean b=this.updateById(w2);
            if(a&&b){
                map.put("code","1");
                map.put("msg","存入成功");
            }else{
                throw new RuntimeException();
            }
        }
        return map;
    }
}
